import JsBridge from '@/utils/JsBridge.js'
import { createRouter, createWebHashHistory } from "vue-router";
import { setStore, globalLoading, getStore } from "../utils/utils";
let isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

const routes = [
  {
    path: "/",
    component: () => import("@/views/login"),
  },
  {
    path: "/forget",
    component: () => import("@/views/login/forget"),
  },
  {
    path: "/register",
    component: () => import("@/views/login/register"),
  },
  {
    path: "/shareH5",
    component: () => import("@/views/shareH5"),
  },
  {
    path: "/realName",
    component: () => import("@/views/realName"),
  },
  {
    path: "/home",
    component: () => import("@/views/home"),
    redirect: "/index",
    children: [
      // 首页
      {
        path: "/index",
        name: "首页",
        component: () => import("@/views/index"),
        meta: {
          openName: "/index",
        },
      },
      {
        path: "/device",
        name: "设备详情",
        component: () => import("@/views/index/device"),
      },
      {
        path: "/device/updateName",
        name: "修改设备名称",
        component: () => import("@/views/index/device/updateName"),
      },
      {
        path: "/device/add",
        name: "流量卡新增",
        component: () => import("@/views/index/device/add"),
      },

      // 国内卡
      {
        path: "/inChina",
        name: "国内服务详情",
        component: () => import("@/views/index/inChina"),
      },
      {
        path: "/inChina/rechargeRecord",
        name: "国内-订购记录",
        component: () => import("@/views/index/inChina/rechargeRecord"),
      },
      {
        path: "/inChina/rechargeRecordDetail",
        name: "国内-订购记录-详情",
        component: () => import("@/views/index/inChina/rechargeRecord/detail"),
      },
      {
        path: "/inChina/internetRecord",
        name: "国内-流量账单",
        component: () => import("@/views/index/inChina/internetRecord"),
      },

      // 国际卡
      {
        path: "/outChina",
        name: "国际卡详情",
        component: () => import("@/views/index/outChina"),
      },
      {
        path: "/outChina/rechargeRecord",
        name: "国际-订购记录",
        component: () => import("@/views/index/outChina/rechargeRecord"),
      },
      {
        path: "/outChina/rechargeRecordDetail",
        name: "国际-订购记录-详情",
        component: () => import("@/views/index/outChina/rechargeRecord/detail"),
      },

      // 套餐
      {
        path: '/meal',
        name: '套餐管理',
        component: () => import("@/views/meal"),
      },
      {
        path: "/meal",
        name: "国内套餐",
        component: () => import("@/views/meal/inChinaMeal"),
      },
      {
        path: "/meal/pack",
        name: "国内加油包",
        component: () => import("@/views/meal/inChinaMeal/pack"),
      },
      {
        path: "/outChinaMeal",
        name: "国际套餐",
        component: () => import("@/views/meal/outChinaMeal"),
      },
      {
        path: "/outChinaMeal/detail",
        name: "国际套餐详情",
        component: () => import("@/views/meal/outChinaMeal/detail"),
      },
      {
        path: "/outChinaMeal/list",
        name: "国际套餐列表",
        component: () => import("@/views/meal/outChinaMeal/list"),
      },
      {
        path: "/outChinaMeal/search",
        name: "国际套餐搜索",
        component: () => import("@/views/meal/outChinaMeal/search"),
      },

      // 客服
      {
        path: "/customer",
        name: "客服",
        component: () => import("@/views/customer"),
        meta: {
          openName: "/customer",
        },
      },

      // 我的
      {
        path: "/mine",
        name: "我的",
        component: () => import("@/views/mine"),
        meta: {
          openName: "/mine",
        },
      },
      {
        path: "/order",
        name: "我的订单",
        component: () => import("@/views/mine/order"),
      },
      {
        path: "/orderDetail",
        name: "订单详情",
        component: () => import("@/views/mine/order/detail"),
      },
      {
        path: "/account",
        name: "账号与安全",
        component: () => import("@/views/mine/account"),
      },
      {
        path: "/changePassword",
        name: "更换密码",
        component: () => import("@/views/mine/changePassword"),
      },
      {
        path: "/changePhone",
        name: "更换手机号",
        component: () => import("@/views/mine/changePhone"),
      },
      {
        path: "/about",
        name: "关于智享",
        component: () => import("@/views/mine/about"),
      },
      {
        path: "/help",
        name: "帮助中心",
        component: () => import("@/views/help"),
      },
    ]
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 设置头的字体颜色
let darkTextColorPath = ['/index',]
export const setStatusBarTextColor = (color) => {
  JsBridge.callHandler('setStatusBarTextColor', color, (res) => {
    console.log('setStatusBarTextColor 响应值 ： ', res);
  })
}

let login = false
const shartPath = []

router.beforeEach((to, from, next) => {
  let userInfo = getStore('USER_INFO')

  // 分享不走基座
  if (shartPath.includes(to.path)) {
    next()
    return
  }

  if (!isAndroid && !isiOS) {
    next()
    return
  }

  if (darkTextColorPath.includes(to.path)) {
    setStatusBarTextColor('light')
  } else {
    setStatusBarTextColor('dark')
  }


  // 第一次加载app，判断是否免登录
  if (!login) {
    login = true
    if (userInfo && userInfo.token) {
      next({ path: "/index" });
    } else {
      next();
    }
  } else {
    next();
  }
});

router.afterEach((to, from) => {
  globalLoading(false);
});

export default router;